﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTCustomerUI : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private Int32 CustomerID = HangSo.DEFAULT_ID;
        private String message = "";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    CustomerID = Int32.Parse(Request.QueryString["CustomerID"]);
                }
                catch
                {

                }

                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                if (CustomerID != -1)
                {
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Edit_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Edit_Other;
                    KhachHang theKhachHang = KhachHangProvider.GetKhachHangByKhachHangID(CustomerID);
                    txtTenVN.Text = theKhachHang.CustomerVN;
                    txtTenOther.Text = theKhachHang.CustomerOrder;
                    txtThuTu.Text = theKhachHang.NumberOfOrder.ToString();
                    Imgbanner.ImageUrl = "~/ImageDatas/Customers/" + theKhachHang.CustomerIMG;
                    txtAddress.Text = theKhachHang.AddressVN;
                    txtAddressOther.Text = theKhachHang.AddressOther;
                    txtEmail.Text = theKhachHang.Email;
                    txtPhone.Text = theKhachHang.Phone;
                    txtWeb.Text = theKhachHang.CustomerURL;

                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["HinhAnhID"] = CustomerID;
                    ViewState["HinhAnhUrl"] = theKhachHang.CustomerIMG;
                    ViewState["Publish"] = theKhachHang.Publish;
                }
                else
                {
                    ViewState["action"] = HangSo.ACTION_ADD;
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Add_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Add_Other;
                }
            }
        }
        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbApply.Text = rm.GetString("lbApply", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbSave.Text = rm.GetString("lbSave", ci);            
            lbCustomerManagers.Text = rm.GetString("lbCustomerManagers", ci);

            lbDiaChiVN.Text = rm.GetString("lbDiaChiVN", ci);
            lbDiaChiOther.Text = rm.GetString("lbDiaChiOther", ci);
            lbThuTu.Text = rm.GetString("lbThuTu", ci);
            lbHinhAnh.Text = rm.GetString("lbHinhAnh", ci);

            lbPhone.Text = rm.GetString("lbPhone", ci);
            lbTenOther.Text = rm.GetString("lbTenOther", ci);
            lbTenVN.Text = rm.GetString("lbTenVN", ci);           
        }

        /// <summary>
        /// Kiem tra du lieu
        /// </summary>
        /// <returns></returns>
        public Boolean ValidateData()
        {
            Boolean flag = true;

            if (txtTenVN.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }
            //if (txtTenOther.Text == "")
            //{
            //    if (IsVN == true)
            //        message = HangSo.check_VN;
            //    else
            //        message = HangSo.check_Other;
            //    flag = false;
            //}
            try
            {
                Int32.Parse(txtThuTu.Text);
            }
            catch
            {
                flag = false;
            }

            return flag;
        }






        /// <summary>
        /// Upload anh va resize kich thuoc anh
        /// </summary>
        /// <param name="theKhachHang"></param>
        /// <returns></returns>
        public String UploadFile(KhachHang theKhachHang)
        {
            String action = ViewState["action"].ToString();
            if (this.FileUploadImage.HasFile)
            {
                String fileName = FileUploadImage.PostedFile.FileName;
                String pathAvatars = Server.MapPath("~/");
                String fileType = Path.GetExtension(fileName);
                String FileNewName = DateTime.Now.Ticks.ToString() + fileType;
                try
                {
                    System.Drawing.Image oImg = System.Drawing.Image.FromStream(FileUploadImage.PostedFile.InputStream);
                    if (Int32.Parse(oImg.Width.ToString()) > 1000)
                    {


                        int thumbWidth = 1000;
                        int thumbHeight = (Int32.Parse(oImg.Height.ToString()) * thumbWidth) / (Int32.Parse(oImg.Width.ToString()));
                        System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);
                        Graphics oGraphic = Graphics.FromImage(oThumbNail);
                        oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                        oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                        oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
                        oGraphic.DrawImage(oImg, oRectangle);
                        oThumbNail.Save(pathAvatars + "\\ImageDatas\\Customers\\" + FileNewName);
                        theKhachHang.CustomerIMG = FileNewName;
                        oImg.Dispose();
                    }

                    else
                    {

                        this.FileUploadImage.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Customers//" + FileNewName);
                        theKhachHang.CustomerIMG = FileNewName;
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.Message);
                }
            }
            else
            {
                if (action != HangSo.ACTION_ADD)
                {
                    theKhachHang.CustomerIMG = ViewState["HinhAnhUrl"].ToString();
                }
                else
                {
                    theKhachHang.CustomerIMG = "noImage.gif";
                }

            }
            return theKhachHang.CustomerIMG;
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 CustomerIDOut = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                KhachHang theKhachHang = new KhachHang();
                theKhachHang.CustomerOrder = txtTenOther.Text;
                theKhachHang.CustomerVN = txtTenVN.Text;
                theKhachHang.NumberOfOrder = Int32.Parse(txtThuTu.Text);
                theKhachHang.CustomerIMG = UploadFile(theKhachHang);
                theKhachHang.CustomerURL = txtWeb.Text;
                theKhachHang.AddressOther = txtAddressOther.Text;
                theKhachHang.AddressVN = txtAddress.Text;
                theKhachHang.Email = txtEmail.Text;
                theKhachHang.Phone = txtPhone.Text;

                if (action == HangSo.ACTION_ADD)
                {
                    theKhachHang.CustomerID = -1;
                    theKhachHang.Publish = true;
                    CustomerIDOut = KhachHangProvider.IUKhachHang(theKhachHang, true);
                }
                else
                {
                    theKhachHang.CustomerID = Int32.Parse(ViewState["HinhAnhID"].ToString());
                    theKhachHang.Publish = Boolean.Parse(ViewState["Publish"].ToString());
                    CustomerIDOut = KhachHangProvider.IUKhachHang(theKhachHang, false);
                }
            }
            String str = "";
            if (IsVN == true)
                str = HangSo.App_VN;
            else
                str = HangSo.App_Other;
            Response.Redirect("EHTCustomerUI.aspx?CustomerID=" + CustomerIDOut.ToString()+"&&Message="+str);
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 CustomerIDOut = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                KhachHang theKhachHang = new KhachHang();
                theKhachHang.CustomerOrder = txtTenOther.Text;
                theKhachHang.CustomerVN = txtTenVN.Text;
                theKhachHang.NumberOfOrder = Int32.Parse(txtThuTu.Text);
                theKhachHang.CustomerIMG = UploadFile(theKhachHang);
                theKhachHang.CustomerURL = txtWeb.Text;
                theKhachHang.AddressOther = txtAddressOther.Text;
                theKhachHang.AddressVN = txtAddress.Text;
                theKhachHang.Email = txtEmail.Text;
                theKhachHang.Phone = txtPhone.Text;

                if (action == HangSo.ACTION_ADD)
                {
                    theKhachHang.CustomerID = -1;
                    theKhachHang.Publish = true;
                    CustomerIDOut = KhachHangProvider.IUKhachHang(theKhachHang, true);
                }
                else
                {
                    theKhachHang.CustomerID = Int32.Parse(ViewState["HinhAnhID"].ToString());
                    theKhachHang.Publish = Boolean.Parse(ViewState["Publish"].ToString());
                    CustomerIDOut = KhachHangProvider.IUKhachHang(theKhachHang, false);
                }
            }
            Response.Redirect("EHTCustomers.aspx");
        }
    }
}